import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales'

// 简化的表单配置，实际项目中应该有完整的字段配置
export function useFormSchema() : VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.seckill.activity.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.seckill.activity.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.seckill.activity.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.seckill.activity.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'name',
      title: $t('views.mall.promotion.seckill.activity.name'),
      minWidth: 140,
    },
    {
      field: 'configIds',
      title: $t('views.mall.promotion.seckill.activity.periods'),
      minWidth: 220,
      slots: { default: 'configIds' },
    },
    {
      field: 'startTime',
      title: $t('views.mall.promotion.seckill.activity.startTime'),
      minWidth: 210,
      slots: { default: 'timeRange' },
    },
    {
      field: 'picUrl',
      title: $t('views.mall.promotion.seckill.activity.picUrl'),
      minWidth: 80,
      cellRender: {
        name: 'CellImage',
      },
    },
    {
      field: 'spuName',
      title: $t('views.mall.promotion.seckill.activity.spuName'),
      minWidth: 300,
    },
    {
      field: 'marketPrice',
      title: $t('views.mall.promotion.seckill.activity.marketPrice'),
      minWidth: 100,
      formatter: ({ row }) => `￥${(row.marketPrice / 100).toFixed(2)}`,
    },
    {
      field: 'seckillPrice',
      title: $t('views.mall.promotion.seckill.activity.seckillPrice'),
      minWidth: 100,
      formatter: ({ row }) => {
        if (!(row.products || row.products.length === 0)) {
          return '￥0.00';
        }
        const seckillPrice = Math.min(
          ...row.products.map((item: any) => item.seckillPrice),
        );
        return `￥${(seckillPrice / 100).toFixed(2)}`;
      },
    },
    {
      field: 'status',
      title: $t('common.status'),
      align: 'center',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'stock',
      title: $t('views.mall.promotion.seckill.activity.stock'),
      align: 'center',
      minWidth: 80,
    },
    {
      field: 'totalStock',
      title: $t('views.mall.promotion.seckill.activity.totalStock'),
      align: 'center',
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      align: 'center',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      align: 'center',
      width: 150,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
